<?

$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0; $cnt_i = 0;

/* Grab Em All */

$urllistAll[]='http://www.fanfiction.net/atom/u/'.$_GET['aid'].'/';

while(list($k,$url)=each($urllistAll)){
	$fdat =  file ( $url );
	$full_data = '';
	if(is_array($fdat)){

		echo count($fdat)." lines in raw data for: ".$url."<br>";
		flush();
		include('ffnetRSS.parse.php');
	}
	//var_dump($dataAts);
	//echo "<hr>";
	$dataA= $dataAts;
	$dataAts=array();
}

/*echo '<pre>';
var_dump($dataA);
echo '</pre>';
exit();*/


echo "Get All Scan Complete: ".count($id_list)." stories found.<br>";

$sqLST = "SELECT story_id,chapters,updated,status FROM fic_listing WHERE sitekey='F' and story_id IN ('".join("','",$id_list)."')";
//echo $sqLST."<br>";
$rsLST = $db->Execute($sqLST);
$aLST = $rsLST->GetAssoc();
//$found = count($dataA);
$poss = count($aLST);
echo $poss." Get All stories in potential updates<br>";
//exit();

while(list($key,$val)=each($dataA)){
	if(isset($aLST[$val['id']])){		
				$upd = false;
				if($aLST[$val['id']]['chapters']<$val['chapters']){ $upd = true; }
				if($aLST[$val['id']]['updated']!=$val['updated']){ $upd = true; }
				$cnta=$cnta+1;
				
				if($upd){
					$adQ = '';
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					

					if($val['updated']!='20--'){					
						if($aLST[$val['id']]['status']=='?' || $aLST[$val['id']]['status']=='N'){
							$cnt=$cnt+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']!='I' && $aLST[$val['id']]['status']!='B' ){	
							$cnt=$cnt+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']=='I'){
								$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='F' and story_id='".$val['id']."'";													
						}elseif( $aLST[$val['id']]['status']=='D' || $aLST[$val['id']]['status']=='B'){	
								$cnt1=$cnt1+1;
								$sqlU = "UPDATE fic_listing SET ".$sqT.",status='?',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
								$ffUD = $ffUD+1;
						}
//echo $sqlU."<br>";
						
						$db->Execute($sqlU);
						
						
					}else{
					}
				}else{
				}			
	}else{
		$sql = "INSERT INTO `fic_listing` ( `story_id` , `author_id` , `story_title` , `chapters` , `published` , `updated` , `status`,`summary`,`inserted`,sitekey ) VALUES ('".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."', '".$val['published']."', '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW(),'F')";
		$cnt2 = $cnt2 + 1;
//echo $sql."<br>";
		$rs1 = $db->Execute($sql);
		
		$sql2 = "INSERT INTO `fic_authors` ( `author_id` , `author_name`,sitekey )VALUES ('".$val['author_id']."', '".str_replace("'","''",$val['author'])."','F')";
//echo $sql2."<br>";

		$rs2 = $db->Execute($sql2);

	}
}



//exit();



echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;total entries:'.$cnt.', ignored:'.$cnt_i.', new stories:'.$cnt_n.', updated new:'.$cnt_Q.', UPDATED:'.$cnt_u.'<br>';

$db->Execute("UPDATE fic_authors SET linkid=id WHERE linkid='0'");

?>
